package com.wu.mp.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.wu.mp.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * className: DruidDBConfig <br/>
 * author: xwu36 <br/>
 * date: 2019/7/2 15:43 <br/>
 * describe:
 *
 * @author xwu36
 */
@Slf4j
@Configuration
public class DruidDataBaseConfig {

    @Bean
    public ServletRegistrationBean druidServlet() {
        log.info("配置Druid的servlet");
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        // IP白名单
        registrationBean.addInitParameter("allow", "");
        // IP黑名单(共同存在时，deny优先于allow)
        registrationBean.addInitParameter("deny", "");
        //控制台管理用户
        registrationBean.addInitParameter("loginUsername", Constants.DRUID_USERNAME);
        registrationBean.addInitParameter("loginPassword", Constants.DRUID_PASSWORD);
        //是否能够重置数据 禁用HTML页面上的“Reset All”功能
        registrationBean.addInitParameter("resetEnable", Constants.DRUID_NO_REST);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}
